package com.easybuilder.core.menu.controller;

import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.utils.StringUtil;
import com.easybuilder.core.menu.entity.SysMenu;
import com.easybuilder.core.menu.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZXS
 * @version 1.0
 * @description 类
 * @date 2021-6-20 14:36
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/getAll")
    public Result<Object> getAll() {
        List<SysMenu> list = menuService.getAll();
        return OK(list);
    }

    @GetMapping("/load/{pid}")
    public Result<Object> loadByPid(@PathVariable(name = "pid")String pid) {
        List<SysMenu> list = new ArrayList<>();
        if (StringUtil.isNull(pid)){
            list = menuService.loadByPid(null);
        } else {
            list = menuService.loadByPid(Long.valueOf(pid));
        }
        return OK(list);
    }
    @PostMapping("/save")
    public Result<Object> save(@RequestBody SysMenu sysMenu){
        if (sysMenu == null) {
            return Error(HttpStatus.BAD_REQUEST,"参数异常");
        }
        menuService.save(sysMenu);
        return OK("保存成功");
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable(name = "id", required = true)Long id){
        menuService.delete(id);
        return OK("删除成功");
    }
}
